// JavaScript Document

/**
 * import compontent 
 */
Vsg.importClass(
	[
	]
);

Vsg.loadExtLibs([
	"ux/RowExpander.js"
	]
);


function getSelectedCategoryPanel(title , id ) {

	var sm = new Ext.grid.CheckboxSelectionModel({
		singleSelect:false
	});
	
	var store = new Ext.data.JsonStore({
    	// store configs
    	autoLoad : true,
    	url: UrlMKey['stk-assembly-selectedCate'],
    	storeId: 'cateSelectedCategoryPanel',
    	// reader configs
    	root: 'rows',
    	idProperty: 'cateId',  
    	fields: [
			{name:'description'},
			{name:'type'},
			{name:'startDate'},
			{name:'cycle'}
		]
	});
	
	try {
		// --- grid panel ---
  		var grid = new Ext.grid.GridPanel({
        	store: store,
			id:id,
			title:title,			
			maskDisabled:false,
        	loadMask: true,        	
        	// grid columns
			colModel:new Ext.grid.ColumnModel([
				sm,
				{
					header: "板块名称",
					width: 100,
					sortable: false,
					dataIndex: "description"
				},
				{
					header: "类 型",
					sortable: false, 
					dataIndex: "type"
				},
				{
					header: "开始日期",
					sortable: false, 
					dataIndex: "startDate"
				},
				{
					header: "周 期",
					sortable: false, 
					dataIndex: "cycle"
				}												
			]),
			sm:sm,
        	// customize view config
        	viewConfig: {
            	forceFit:true
        	},			
        	stripeRows: true

    	});
		//grid.on("rowclick" , Vsg.getDefinedEvent("DateList.evt_rowclick"));				
		return grid;	
	} catch (e) {
		alert("Get model panel error: ");
	}
	
}



function getAnalyticalStocksPanel(url) {
	
	
    var expander = new Ext.ux.grid.RowExpander({
        tpl : new Ext.Template(
            '<p><b>出现种类/板块:</b> {assemblyCates}</p>'
        )
    });

	var store = new Ext.data.JsonStore({
    	// store configs
    	autoLoad : true,
    	url: url,
    	storeId: 'cateStockStore',
    	// reader configs
    	root: 'rows',
    	idProperty: 'stkCode',  
    	fields: [
			{name:'stockCode', mapping:'stkCode'},
			{name:'stockName', mapping:'stkName'},
			{name:'rate', type: 'float'},
			{name:'shownDate'},
			{name:'assemblyCates', type:'string'},
			{name:'assemblyDates', type:'string'}
		]
	});
	
	try {
		// --- grid panel ---
  		var grid = new Ext.grid.GridPanel({
        	store: store,
			title:'分析后关注的股票',
			id:'stkCateAssemblyStkPanel',			
			maskDisabled:false,
        	loadMask: true,        	
        	// grid columns
			colModel:new Ext.grid.ColumnModel([
				expander,
				{
					header: "股票代码",
					width: 100,
					sortable: false,
					dataIndex: "stockCode"
				},
				{
					header: "股票名称",
					sortable: false, 
					dataIndex: "stockName"
				},
				{
					header: "最近出现日期",
					sortable: false, 
					dataIndex: "shownDate"
				},				
				{
					header: "涨 幅",
					sortable: false, 
					dataIndex: "rate",
					renderer:function(val) {
						var result = (val*100).toFixed(2);
						return result+"%";
					}
				}								
			]),
        	// customize view config
        	viewConfig: {
            	forceFit:true
        	},
			plugins: expander,		
        	stripeRows: true

    	});
	
		return grid;	
	} catch (e) {
		alert("Get model panel error: ");
	}	
}


/** ===============================
 * init ui interface 
 * ================================
 */
function init(){

	try {
		
		// --- set layout content -----
		var $layoutContent = {
			layout:"border",
			items:[
				{
					layout:"border",
					region:'center',
					tbar:[{
						text:'分析筛选',
						handler:Vsg.getDefinedEvent("evt_filter_analysed")
					}],
					items:[
					{
						region:'west',
						width:430,	
						layout:'fit',
						split:true,
						margins:"5 0 5 5 ",
						border:false,
						items:[
							getSelectedCategoryPanel("关注板块/种类","attentionCate")
						]
					},
					{
						region:'center',
						layout:'fit',
						border:false,
						margins:"5 5 5 0",
						items:[
							getAnalyticalStocksPanel(UrlMKey['stk-assembly-freq-stocklist'])
						]
					}					
					]
				}
			]
		};

    	new Ext.Viewport( $layoutContent );
		
	} catch (e) {
		alert('出现系统性错误，请联系管理人员!');
		//Ext.log("Initial UI error: " + e);
	}
}
// --- run the init method
Vsg.run( init );


/**
 * event
 * @param {Object} $this
 * @param {Object} rowIndex
 * @param {Object} event
 */
Vsg.definedEvent("evt_filter_analysed" , function($this, e) {
	var attentionCatePanel = Ext.getCmp("attentionCate");
	
	var selModel = attentionCatePanel.getSelectionModel();	
	var selRecs = selModel.getSelections();

	function resultHandle($result) {
		
		if ($result == "yes") {
			

			
			var recPass = [];
			for (var $i = 0 ; $i < selRecs.length ; $i++) {
				recPass.push( Ext.util.JSON.encode(selRecs[$i].data) );
			}
			
			try {
				// load config object 

				var $actionConfig = {
					url:UrlMKey['assembly-analysisSelectCate'],
					method:"POST",
					timeout:30,
					params:{
						records:recPass
					}
				};
				$actionConfig.success = function($form , $action) {

					Ext.MessageBox.show({
        				title: "提示",
        				msg: "删除已经成功!",
        				buttons: Ext.MessageBox.OK,
        				icon: Ext.MessageBox.WARNING
       				});
			
					try {
						
						// ---- check the message ----

						
					} catch (ex) {
						alert("error handle : " + ex);
					} finally {
						// --- close the window
					
					}

				}


				$actionConfig.failure = function($form , $action) {
					Ext.MessageBox.show({
        				title: "&#37819;&#24878;&#12378;",
        				msg: "&#37714;&#29371;&#27342;&#28051;&#23942;&#22426;&#37716;&#65533;154654",
        				buttons: Ext.MessageBox.OK,
        				icon: Ext.MessageBox.WARNING
       				});	
				}

				
				Ext.Ajax.request($actionConfig);
				
			} catch ( ex ) {
				alert( ex );
			}			
			
			
		} else {
		}
		
	}
	
	try {
		
		if (selModel.getCount() == 0) {
			throw "没有选择一条数据!";
		}
		
		Ext.MessageBox.confirm("分析确认", "是否分析选择数据", resultHandle);		
		
	} catch ( ex ) {
		
		alert(ex);
	
	}
	
});
